package com.tianyu.inspection.controller;

import com.tianyu.inspection.common.Constants;
import com.tianyu.inspection.common.enums.RoState;
import com.tianyu.inspection.common.enums.Role;
import com.tianyu.inspection.common.annotation.Permission;
import com.tianyu.inspection.entity.DangerType;
import com.tianyu.inspection.entity.model.OperateMo;
import com.tianyu.inspection.entity.model.QueryMo;
import com.tianyu.inspection.entity.vo.Ro;
import com.tianyu.inspection.entity.vo.ServiceRo;
import com.tianyu.inspection.service.api.IDangerTypeService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

/**********************************
 * @Author YSW
 * @Description 施工单位接口
 * @Date 2021/1/15 - 18:12
 **********************************/

@RestController
@RequestMapping("/api/2.0/dangerType")
public class DangerTypeController {

    @Resource
    private IDangerTypeService dangerTypeService;


    @GetMapping(value = "/list")
    @Permission(value = 4,tags = Constants.API_PATROL)
    public Ro<Object> getList(HttpServletRequest request,@RequestHeader("u-projectId") Integer projectId){
        QueryMo queryMo = QueryMo.getQueryMo(request);
        int roleId = (int) request.getAttribute("roleId");
        Ro<Object> ro = new Ro<>();
        queryMo.build(DangerType.class,null);
        if (roleId == Role.ROLE_SUPER_ADMIN.getValue()){
            return ro.setContent(dangerTypeService.searchListPage(queryMo));
        } else {
            return ro.setContent(dangerTypeService.searchList(queryMo,projectId));
        }
    }

    @PostMapping(value = "/admin/insert")
    @Permission(value = 1,tags = Constants.API_SUPER)
    public Ro<DangerType> insert(@RequestBody OperateMo operateMo){
        Ro<DangerType> ro = new Ro<>();
        if (operateMo.get("name") != null){
            operateMo.put("typeName",operateMo.get("name"));
        }
        operateMo.createBuild(DangerType.class,null);
        if (operateMo.getEntity().size()==0){
            return ro.setResultEnum(RoState.FAIL).setMessage("无操作参数传入,请传参后重试");
        }
        if (!dangerTypeService.insert(operateMo)){
            ro.setResultEnum(RoState.FAIL);
        }
        return ro;
    }

    @PatchMapping(value = "/admin/modify")
    @Permission(value = 1,tags = Constants.API_SUPER)
    public Ro<DangerType> modify(@RequestBody OperateMo operateMo){
        Ro<DangerType> ro = new Ro<>();
        if (operateMo.get("name") != null){
            operateMo.put("typeName",operateMo.get("name"));
        }
        final ServiceRo serviceRo = operateMo.updateBuild(DangerType.class, null);
        if (!serviceRo.getResult()){
            return ro.setResultEnum(RoState.FAIL).setMessage(serviceRo.getMessage());
        }
        if (operateMo.getEntity().size()==0){
            return ro.setResultEnum(RoState.FAIL).setMessage("无操作参数传入,请传参后重试");
        }
        if (Objects.isNull(operateMo.get("id"))){
            return ro.setResultEnum(RoState.FAIL).setMessage("缺少[id]字段,请传参后重试");
        }
        if (!dangerTypeService.update(operateMo)){
            ro.setResultEnum(RoState.FAIL).setMessage("参数无匹配,修改失败");
        }
        return ro;
    }

    @DeleteMapping(value = "/admin/delete/{id}")
    @Permission(value = 1,tags = Constants.API_SUPER)
    public Ro<DangerType> delete(@PathVariable Integer id){
        Ro<DangerType> ro = new Ro<>();
        QueryMo queryMo = new QueryMo();
        queryMo.put("id",id);
        queryMo.build(DangerType.class,null);
        if (!dangerTypeService.delete(queryMo)){
            ro.setResultEnum(RoState.FAIL).setMessage("无匹配ID,删除失败");
        }
        return ro;
    }

    @DeleteMapping(value = "/admin/delete/logical/{id}")
    @Permission(value = 1,tags = Constants.API_SUPER)
    public Ro<DangerType>  logicallyDelete(@PathVariable Integer id){
        Ro<DangerType> ro = new Ro<>();
        QueryMo queryMo = new QueryMo();
        queryMo.put("id",id);
        queryMo.build(DangerType.class,null);
        if (!dangerTypeService.logicallyDelete(queryMo)){
            ro.setResultEnum(RoState.FAIL).setMessage("无匹配ID,删除失败");
        }
        return ro;
    }
}
